package com.klm.easymq.demo.service;

import com.klm.easymq.annotation.MQListener;
import com.klm.easymq.demo.entity.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 通知服务类
 * 演示Easy-MQ SDK的另一个消息消费者
 */
@Slf4j
@Service
public class NotificationService {
    
    private final AtomicInteger notificationCount = new AtomicInteger(0);
    
    /**
     * 监听订单创建消息，发送通知
     */
    @MQListener(
        topic = "order.created",
        group = "notification-service",
        idempotentKey = "#order.orderId"
    )
    public void handleOrderCreated(Order order) {
        log.info("发送订单创建通知: orderId={}, userId={}, amount={}", 
                order.getOrderId(), order.getUserId(), order.getAmount());
        
        // 模拟发送通知逻辑
        sendEmailNotification(order);
        sendSmsNotification(order);
        
        notificationCount.incrementAndGet();
    }
    
    /**
     * 监听订单取消消息，发送取消通知
     */
    @MQListener(
        topic = "order.cancelled",
        group = "notification-service",
        idempotentKey = "#order.orderId"
    )
    public void handleOrderCancelled(Order order) {
        log.info("发送订单取消通知: orderId={}, userId={}, reason={}", 
                order.getOrderId(), order.getUserId(), order.getRemark());
        
        // 模拟发送取消通知
        sendCancellationNotification(order);
        
        notificationCount.incrementAndGet();
    }
    
    /**
     * 监听订单更新消息，发送状态变更通知
     */
    @MQListener(
        topic = "order.updated",
        group = "notification-service",
        idempotentKey = "#order.orderId"
    )
    public void handleOrderUpdated(Order order) {
        log.info("发送订单状态变更通知: orderId={}, userId={}, status={}", 
                order.getOrderId(), order.getUserId(), order.getStatus());
        
        // 模拟发送状态变更通知
        sendStatusChangeNotification(order);
        
        notificationCount.incrementAndGet();
    }
    
    /**
     * 模拟发送邮件通知
     */
    private void sendEmailNotification(Order order) {
        log.info("📧 发送邮件通知: 用户{}的订单{}创建成功，金额{}", 
                order.getUserId(), order.getOrderId(), order.getAmount());
    }
    
    /**
     * 模拟发送短信通知
     */
    private void sendSmsNotification(Order order) {
        log.info("📱 发送短信通知: 用户{}的订单{}创建成功，金额{}", 
                order.getUserId(), order.getOrderId(), order.getAmount());
    }
    
    /**
     * 模拟发送取消通知
     */
    private void sendCancellationNotification(Order order) {
        log.info("❌ 发送取消通知: 用户{}的订单{}已取消，原因{}", 
                order.getUserId(), order.getOrderId(), order.getRemark());
    }
    
    /**
     * 模拟发送状态变更通知
     */
    private void sendStatusChangeNotification(Order order) {
        log.info("🔄 发送状态变更通知: 用户{}的订单{}状态变更为{}", 
                order.getUserId(), order.getOrderId(), order.getStatus());
    }
    
    /**
     * 获取已发送的通知数量
     */
    public int getNotificationCount() {
        return notificationCount.get();
    }
} 